<?php

namespace app\user\controller;
use app\common\controller\UserBase;
use app\common\model\Customer as Kehu;
use app\common\model\CustomerTrack;
use app\common\model\CustomerRemind;
use app\common\model\UserConfig;
use app\common\model\CustomerOrder;

class Customer extends UserBase
{
    protected function _initialize()
    {
        parent::_initialize();
        !config('website_status') && die(config('colse_explain'));
        $config = cache('db_config_data');
        if (!$config) {
            $config = [];
            foreach (model('config')->select() as $v) {
                $config[$v['group']][$v['name']] = $v['value'];
            }
            cache('db_config_data', $config);
        }
        config($config);
    }
    //客户列表
    public function index()
    {
        default_config('customer');//默认客户参数
        $customer = new Kehu();
        if (input('cname')) {
            $customer ->where('cname like "%' . input('cname') .'%"');
        }
        //筛选
        if(input('type')){
            $customer ->where('type='.input('type'));
        }
        //筛选
        if(input('intention')){
            $customer ->where('intention='.input('intention'));
        }
        //筛选
        if(input('come_from')){
            $customer ->where('come_from='.input('come_from'));
        }
        //筛选
        if(input('status')){
            $customer ->where('status='.input('status'));
        }
        //筛选
        $type = UserConfig::where(['name'=>'type','uid'=>session('user_auth.user_id')])->find();
        $status = UserConfig::where(['name'=>'status','uid'=>session('user_auth.user_id')])->find();
        $come_from = UserConfig::where(['name'=>'come_from','uid'=>session('user_auth.user_id')])->find();
        $intention = UserConfig::where(['name'=>'intention','uid'=>session('user_auth.user_id')])->find();
        
        $list = $customer->order('id desc')->paginate(config('page_number'));
        return $this->fetch('index',[
            'list' => $list,
            'type' => $type,
            'status' => $status,
            'come_from' => $come_from,
            'intention' => $intention,
        ]);
    }
    //查看客户
    public function show()
    {
        $id = input('id');
        //客户信息
        $customer = Kehu::get($id);
        //跟踪信息
        $track = CustomerTrack::where(['cid'=>$customer['id']])->order('id desc')->paginate(6);
        //提醒信息
        $remind = CustomerRemind::where(['cid'=>$customer['id']])->order('id desc')->limit(10)->select();
        //订单信息
        $order = CustomerOrder::where(['cid'=>$customer['id']])->order('id desc')->limit(10)->select();
        //自定义字段
        $config_customer = model('user_config')->where([ 'group'  => 'customer', 'status' => 1,'is_sys' => '0'])->order('sort_order asc')->select();
        //自定义字段
        $config_track = model('user_config')->where([ 'group'  => 'track', 'status' => 1,'is_sys' => '0'])->order('sort_order asc')->select();
        return $this->fetch('show',[
            'id' => $id,
            'customer' => $customer,
            'track' => $track,
            'remind' => $remind,
            'order' => $order,
            'config_customer' => $config_customer,
            'config_track' => $config_track,
        ]);
    }
    //添加
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['uid'] = session('user_auth.user_id');
            $param['create_time'] = time();
            $param['jdata'] = $param;
            $data = $this->insert('customer', $param);
            
            if ($data === true) {
                insert_user_log('添加了客户："'.$param['cname'].'"');
                $this->success('添加成功', url('user/customer/show',['id' => $this->insertId]));
            } else {
                $this->error($this->errorMsg);
            }
        }
        default_config('customer');//默认客户参数
        //读取字段
        $config = model('user_config')->where([ 'group'  => 'customer', 'status' => 1,])->order('sort_order asc')->select();
        
        $data['province'] = '福建';
        $data['city'] = '福州';
        $data['area'] = '鼓楼区';
        return $this->fetch('add',[
            'data' => $data,
            'config' => $config,
        ]);
    }
    //客户修改
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['jdata'] = $param;
            if (is_array($param['id'])) {
                $data = [];
                foreach ($param['id'] as $v) {
                    $data[] = ['id' => $v, $param['name'] => $param['value']];
                }
                $result = $this->saveAll('customer', $data, input('_verify', true));
            } else {
                $result = $this->update('customer', $param, input('_verify', true));
            }
            if ($result === true) {
                insert_user_log('修改了客户："'.$param['cname'].'"');
                $this->success('修改成功', url('user/customer/show',['id' => $param['id']]));
            } else {
                $this->error($this->errorMsg);
            }
        }
        //读取字段
        $config = model('user_config')->where([ 'group'  => 'customer', 'status' => 1,])->order('sort_order asc')->select();
        return $this->fetch('edit', ['data' => model('customer')->get(input('id')),'config' => $config,]);
    }
    //客户删除
    public function del()
    {
        if ($this->request->isPost()) {
            if ($this->delete('customer', $this->request->param()) === true) {
                insert_user_log('删除了文章');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }
    //客户跟踪列表
    public function track()
    {
        
        $list = CustomerTrack::order('id desc')->paginate(config('page_number'));
        return $this->fetch('track',[
            'list' => $list,
            
        ]);
    }
    //添加跟踪记录
    public function add_track()
    {
        default_config('track');//默认跟踪参数
        $id = input('id');
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $param['cid'] = $id;
            $param['uid'] = session('user_auth.user_id');
            $param['create_time'] = time();
            $param['type'] = $params['type'];
            $param['remark'] = $params['remark'];
            $param['jdata'] = $params;
            $data = $this->insert('customer_track', $param);
            if ($data === true) {
                insert_user_log('添加了客户跟踪记录');
                $this->success('添加成功',url('user/customer/show',['id' => $id]));
            } else {
                $this->error($this->errorMsg);
            }
        }
        //读取字段
        $config = model('user_config')->where([ 'group'  => 'track', 'status' => 1])->order('sort_order asc')->select();
        return $this->fetch('add_track',[
            'config' => $config,
        ]);
    }
    //客户跟踪记录修改
    public function edit_track()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['jdata'] = $param;
            if (is_array($param['id'])) {
                $data = [];
                foreach ($param['id'] as $v) {
                    $data[] = ['id' => $v, $param['name'] => $param['value']];
                }
                $result = $this->saveAll('customer_track', $data, input('_verify', true));
            } else {
                $result = $this->update('customer_track', $param, input('_verify', true));
            }
            if ($result === true) {
                insert_user_log('修改了客户跟踪记录');
                $cus = CustomerTrack::where(['id'=>$param['id']])->find();
                $this->success('修改成功', url('user/customer/show',['id' => $cus['cid']]));
            } else {
                $this->error($this->errorMsg);
            }
        }
        //读取字段
        $config = model('user_config')->where([ 'group'  => 'track', 'status' => 1])->order('sort_order asc')->select();
        return $this->fetch('edit_track', ['config' => $config,'data' => model('customer_track')->get(input('id'))]);
    }
    //跟踪记录删除
    public function del_track()
    {
        if ($this->request->isPost()) {
            if ($this->delete('customer_track', $this->request->param()) === true) {
                insert_user_log('删除了跟踪记录');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }
    //提醒列表
    public function remind()
    {
        $list = CustomerRemind::order('remind_time desc,id desc')->paginate(config('page_number'));
        return $this->fetch('remind',[
            'list' => $list,
        ]);
    }
    //添加提醒
    public function add_remind()
    {
        $id = input('id');
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $param['cid'] = $id;
            $param['uid'] = session('user_auth.user_id');
            $param['create_time'] = time();
            $param['remind_time'] = strtotime($params['remind_time']);
            $param['remark'] = $params['remark'];
            $param['jdata'] = $params['jdata'];
            $data = $this->insert('customer_remind', $param);
            if ($data === true) {
                insert_user_log('添加了客户提醒记录');
                $this->success('添加成功',url('user/customer/show',['id' => $id]));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('add_remind');
    }
    //添加跟踪记录
    public function set_remind()
    {
        $id = input('id');
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['status_time'] = time();

            $data = CustomerRemind::where(['id'=>$id])->update($param);
            insert_user_log('处理了提醒');
            $this->success('处理成功',url('user/customer/remind'));
        }
        return $this->fetch('set_remind');
    }
    //跟踪记录删除
    public function del_remind()
    {
        if ($this->request->isPost()) {
            if ($this->delete('customer_remind', $this->request->param()) === true) {
                insert_user_log('删除了提醒记录');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }
  
    
}
